from enum import StrEnum

from sqlalchemy import func
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column
from models.engine import db
from models.types import StringUUID
from flask_babel import _
class ShopStatus(StrEnum):
    NOT_APPROVED = "N"
    NOT_OPEN = "U"
    STOP = "S"
    WORKING = "W"

    @property
    def label(self)->str|None:
        return {
            "N": _("Enum:ShopStatus:Not approved"),
            "U": _("Enum:ShopStatus:Not open"),
            "S": _("Enum:ShopStatus:Stop"),
            "W": _("Enum:ShopStatus:Working"),
        }.get(self.value)

    @staticmethod
    def choices()->list[str]:
        return ["N","U","S","W"]


class MShop(db.Model):
    __tablename__ = "sp_shop"
    __table_args__ = (
        db.PrimaryKeyConstraint("id", name="sp_shop_pkey"),
        db.Index("sp_shop_shop_type_idx", "shop_type"),
        db.Index("sp_shop_shop_status_idx", "shop_status"),
        db.Index("sp_shop_shop_name_idx", "name"),
        db.Index("sp_shop_shop_master_id_idx", "master_id"),
    )
    id: Mapped[str] = mapped_column(StringUUID, server_default=db.text("uuid_generate_v4()"))
    tenant_id = db.Column(StringUUID, nullable=False,comment="租户ID")
    name = db.Column(db.String(128), nullable=False,comment="店铺名称")
    master_id = db.Column(StringUUID, nullable=False,comment="店主ID")
    master_name = db.Column(db.String(64), nullable=False,comment="店主名称")
    shop_type = db.Column(db.SmallInteger, nullable=False,comment="店铺类型")
    intro = db.Column(db.String(255),nullable=True,comment="店铺简介")
    notice = db.Column(db.String(255),nullable=True,comment="店铺公告")
    industry = db.Column(db.SmallInteger, nullable=True,comment="行业")
    mobile = db.Column(db.String(32), nullable=True,comment="手机")
    service_tel = db.Column(db.String(32), nullable=True,comment="客服电话")
    lat = db.Column(db.Float(precision=24, decimal_return_scale=8), nullable=True,comment="纬度")
    lng = db.Column(db.Float(precision=24, decimal_return_scale=8), nullable=True,comment="经度")
    address = db.Column(db.String(255), nullable=True,comment="详细地址")
    province = db.Column(db.String(32), nullable=True,comment="省")
    city = db.Column(db.String(32), nullable=True,comment="市")
    district = db.Column(db.String(32), nullable=True,comment="区")
    zip_code = db.Column(db.String(24), nullable=True,comment="邮编")
    shop_logo_file_id = db.Column(StringUUID, nullable=True,comment="店铺logo文件ID")
    shop_photos = db.Column(JSONB, nullable=True,comment="店铺相册")
    open_time = db.Column(db.String(128), nullable=True,comment="营业时间")
    shop_status = db.Column(db.String(1),server_default=db.text("'N'::character varying"), nullable=False,comment="店铺状态")
    transport_type = db.Column(db.Boolean, server_default=db.text("false"), nullable=False,comment="运费承担方式：true：商家承担")
    fixed_freight = db.Column(db.Integer, nullable=True,comment="固定运费")
    full_free_shipping = db.Column(db.Integer, nullable=True,comment="满X包邮")
    created_at = db.Column(db.DateTime, server_default=func.current_timestamp(), nullable=False,comment="创建时间")
    created_by = db.Column(StringUUID, nullable=False,comment="创建人")
    updated_at = db.Column(db.DateTime, nullable=True,comment="更新时间")
    updated_by = db.Column(StringUUID, nullable=True,comment="更新人")
    is_distribution = db.Column(db.Boolean, server_default=db.text("false"), nullable=False,comment="是否分销")
    @property
    def ShopPhotos(self)->list[str]:
        if self.shop_photos:
            return self.shop_photos.get('items',[])
        return []

class MShopApproval(db.Model):
    __tablename__ = "sp_shop_approval"
    __table_args__ = (
        db.PrimaryKeyConstraint("id", name="sp_shop_approval_pkey"),
        db.Index("sp_shop_approval_approver_idx", "approver"),
    )
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True,comment="ID")
    shop_id = db.Column(StringUUID, nullable=False)
    approver = db.Column(StringUUID, nullable=False,comment="审核人")
    approve = db.Column(db.Boolean, nullable=False,comment="审核结果")
    content = db.Column(JSONB,server_default=db.text("'{}'::jsonb"),comment="审核内容")
    created_at = db.Column(db.DateTime, nullable=False,server_default=func.current_timestamp())
    updated_at = db.Column(db.DateTime, nullable=True)

class MNotice(db.Model):
    """
    公告表
    """
    __tablename__ = "sp_notice"
    __table_args__ = (
        db.PrimaryKeyConstraint("id", name="sp_notice_pkey"),
        db.Index("idx_sp_notice_status_top", "status","is_top"),
        db.Index("idx_sp_notice_shop", "shop_id"),
    )
    id = db.Column(db.BigInteger,primary_key=True,autoincrement=True,comment="ID")
    shop_id = db.Column(StringUUID, nullable=True,comment="店铺ID")
    title = db.Column(db.String(36), nullable=False,comment="标题")
    content= db.Column(db.Text, nullable=False,comment="内容")
    status = db.Column(db.Boolean,server_default=db.text("true"), nullable=False,comment="发布状态")
    is_top = db.Column(db.Boolean,server_default=db.text("false"), nullable=False,comment="置顶")
    created_at = db.Column(db.TIMESTAMP(timezone=False),server_default=func.current_timestamp(), nullable=False,comment="创建时间")
    created_by = db.Column(StringUUID, nullable=True,comment="创建人")
    updated_at = db.Column(db.TIMESTAMP(timezone=False),server_default=func.current_timestamp(), nullable=False,comment="更新时间")
    updated_by = db.Column(StringUUID, nullable=True,comment="更新人")

class MallMessage(db.Model):
    __tablename__ = "sp_message"
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True,comment="ID")
    account_id = db.Column(StringUUID, nullable=False,comment="账号ID")
    user_name = db.Column(db.String(64), nullable=False,comment="用户名")
    email = db.Column(db.String(128), nullable=True,comment="邮箱")
    contact = db.Column(db.String(64), nullable=True,comment="联系方式")
    content = db.Column(db.Text, nullable=False,comment="内容")
    reply = db.Column(db.Text, nullable=True,comment="回复")
    status = db.Column(db.Boolean, nullable=False, default=False,comment="审核状态")
    created_at = db.Column(db.TIMESTAMP(timezone=False),server_default=func.current_timestamp(), nullable=False,comment="创建时间")
    created_by = db.Column(StringUUID, nullable=False,comment="创建人")

